using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Contact;

public partial class admin_webparts_Plug_ins_Contact_Default : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        string controlName = null;
        if (action != null)
        {
            if (action.Equals("addgroup", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ContactGroupInput.ascx";
            }
            else if (action.Equals("editgroup", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ContactGroupInput.ascx";
            }
            else if (action.Equals("deletegroup", StringComparison.OrdinalIgnoreCase))
            {
                this.DeleteGroup(Convert.ToInt32(Request.QueryString["id"]));
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"], false);
                return;
            }
            else if (action.Equals("configgroup", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ContactGroupConfig.ascx";
            }
            else if (action.Equals("moveconfig", StringComparison.OrdinalIgnoreCase))
            {
                if ("up".Equals(Request.QueryString["move"], StringComparison.OrdinalIgnoreCase))
                {
                    this.MoveConfigUp(Convert.ToInt32(Request.QueryString["id"]));
                }
                else if ("down".Equals(Request.QueryString["move"], StringComparison.OrdinalIgnoreCase))
                {
                    this.MoveConfigDown(Convert.ToInt32(Request.QueryString["id"]));
                }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=configgroup&id=" + Request.QueryString["gid"], false);
                return;
            }
            else if (action.Equals("addconfig", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ContactConfigInput.ascx";
            }
            else if (action.Equals("editconfig", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ContactConfigInput.ascx";
            }
            else if (action.Equals("deleteconfig", StringComparison.OrdinalIgnoreCase))
            {
                this.DeleteConfig(Convert.ToInt32(Request.QueryString["id"]));
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=configgroup&id=" + Request.QueryString["gid"], false);
                return;
            }
            else if (action.Equals("viewgroup", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ContactList.ascx";
            }
            else if (action.Equals("viewcontact", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ContactDetail.ascx";
            }
            else
            {
                controlName = "ContactGroupList.ascx";
            }
        }
        else
        {
            controlName = "ContactGroupList.ascx";
        }

        if (controlName != null)
        {
            Control ctrl = LoadControl(controlName);
            this.Controls.Add(ctrl);
        }
    }

    private void MoveConfigUp(int id)
    {
        try
        {
            ContactGroupConfig item = new ContactGroupConfig(id);
            item.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveConfigDown(int id)
    {
        try
        {
            ContactGroupConfig item = new ContactGroupConfig(id);
            item.MoveDown();
        }
        catch (Exception) { }
    }

    private void DeleteConfig(int id)
    {
        try
        {
            ContactGroupConfig config = new ContactGroupConfig(id);
            config.Delete();
        }
        catch (Exception) { }
    }

    private void DeleteGroup(int id)
    {
        try
        {
            ContactGroup group = new ContactGroup(id);
            group.Delete();
        }
        catch (Exception) { }
    }
}
